package com.njtech.smartuniversity.util;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.io.File;



public class PdfUtilsPic {
    // 利用模板生成pdf
    public static void pdfout(Map<String,String> o, String modalfile, String NewFile) {
        // 模板路径
        String templatePath = modalfile;
        // 生成的新文件路径
        String newPDFPath = NewFile;
        File file = new File(NewFile);

        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            if (!file.exists()) {
                // 先得到文件的上级目录，并创建上级目录，在创建文件
                file.getParentFile().mkdir();
                file.createNewFile();
            }

          /*  BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);*/
            BaseFont bf = BaseFont.createFont("/www/wwwroot/wjxc-master/simfang.ttf" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font FontChinese = new Font(bf);
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();

            form.addSubstitutionFont(bf);
            for(String key : o.keySet()){
                String value = o.get(key);
                form.setField(key,value);
            }

            stamper.setFormFlattening(true);// 如果为false，生成的PDF文件可以编辑，如果为true，生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            Font font = new Font(bf);
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = null;
            ///循环是处理成品只显示一页的问题
            for (int i=1;i<=reader.getNumberOfPages();i++){
                importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
                copy.addPage(importPage);
            }
            /*copy.addPage(importPage);*/
            doc.close();
            System.out.println(newPDFPath);

        } catch (IOException e) {
            System.out.println(e);
        } catch (DocumentException e) {
            System.out.println(e);
        }

    }

    // 利用模板生成word
    public static void wordout(Map<String,String> o, String modalfile, String NewFile) {
        // 模板路径
        String templatePath = modalfile;
        // 生成的新文件路径
        String newPDFPath = NewFile;
        File file = new File(NewFile);

        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            if (!file.exists()) {
                // 先得到文件的上级目录，并创建上级目录，在创建文件
                file.getParentFile().mkdir();
                file.createNewFile();
            }

            /*  BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);*/
            BaseFont bf = BaseFont.createFont("/www/wwwroot/wjxc-master/simfang.ttf" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font FontChinese = new Font(bf);
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();

            form.addSubstitutionFont(bf);
            for(String key : o.keySet()){
                String value = o.get(key);
                form.setField(key,value);
            }

            stamper.setFormFlattening(true);// 如果为false，生成的PDF文件可以编辑，如果为true，生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            Font font = new Font(bf);
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = null;
            ///循环是处理成品只显示一页的问题
            for (int i=1;i<=reader.getNumberOfPages();i++){
                importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
                copy.addPage(importPage);
            }
            /*copy.addPage(importPage);*/
            doc.close();
            System.out.println(newPDFPath);

        } catch (IOException e) {
            System.out.println(e);
        } catch (DocumentException e) {
            System.out.println(e);
        }

    }


}
